package com.xxl.sso.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author xuxueli 2018-03-22 23:41:47
 */
@SpringBootApplication
public class XxlSsoServerApplication {

	public static void main(String[] args) {
        SpringApplication.run(XxlSsoServerApplication.class, args);
	}


	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){

		RedisTemplate<String,Object> redisTemplate=new RedisTemplate();
		redisTemplate.setConnectionFactory(redisConnectionFactory);

		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

		redisTemplate.setKeySerializer(stringRedisSerializer);
		// 注意：使用Jackson2JsonRedisSerializer序列化是无法使用increment()进行加减操作的，因为value在redis中的存储是String字符串类型，
		// 只有StringRedisSerializer、GenericToStringSerializer才可以使用，因为value在redis中的存储是数字类型的。
		redisTemplate.setValueSerializer(objectJackson2JsonRedisSerializer);
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
		redisTemplate.setHashValueSerializer(objectJackson2JsonRedisSerializer);

		// 没有afterPropertiesSet()会报错：Exception in thread "main" java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using it，
		// 但是没有报错。
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

}